﻿namespace Budgetsch.WP7.Utils
{
    using System;
    using System.IO.IsolatedStorage;
    using System.Net;

    using GalaSoft.MvvmLight;

    public class AppSettings
    {

        // Our isolated storage settings
        private readonly IsolatedStorageSettings settings = null;

        // The isolated storage key names of our settings
        private const string IsRememberSettingsKeyName = "IsRememberSettings";
        private const string UserNameSettingKeyName = "UserNameSetting";
        private const string PasswordSettingKeyName = "PasswordSetting";
        private const string CookieContainerSettingsKeyName = "CookieContainerSetting";

        private bool? IsRememberSettingsDefault = null;
        private const string UserNameSettingDefault = null;
        private const string PasswordSettingDefault = null;
        private const CookieContainer CookieSettingsSettingDefault = null;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings()
        {
            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Get the settings for this application.
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (!settings[Key].Equals(value))
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            settings.Save();
        }

        
        public bool? IsRemember
        {
            get
            {
                return GetValueOrDefault(IsRememberSettingsKeyName, IsRememberSettingsDefault);
            }
            set
            {
                if (AddOrUpdateValue(IsRememberSettingsKeyName, value))
                {
                    Save();
                }
            }
        }

        public string UserName
        {
            get
            {
                return GetValueOrDefault(UserNameSettingKeyName, UserNameSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(UserNameSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        public string PasswordSetting
        {
            get
            {
                return GetValueOrDefault(PasswordSettingKeyName, PasswordSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(PasswordSettingKeyName, value))
                {
                    Save();
                }
            }
        }

        public CookieContainer CookieContainer
        {
            get
            {
                return GetValueOrDefault(CookieContainerSettingsKeyName, CookieSettingsSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(CookieContainerSettingsKeyName, value))
                {
                    Save();
                }
            }
        }

    }
}
